﻿namespace Pub.Class
{
    using System;
    using System.Drawing;
    using System.Drawing.Imaging;
    using System.IO;
    using System.Runtime.InteropServices;

    public class Watermark
    {
        private int bottoamSpace;
        private string drawedImagePath;
        private int lucencyPercent = 70;
        private string modifyImagePath;
        private string outPath;
        private int rightSpace;

        public void DrawImage()
        {
            Image image = null;
            Image image2 = null;
            Graphics graphics = null;
            try
            {
                image = Image.FromFile(this.ModifyImagePath);
                image2 = Image.FromFile(this.DrawedImagePath);
                graphics = Graphics.FromImage(image);
                int x = image.Width - this.rightSpace;
                int y = image.Height - this.BottoamSpace;
                float[][] numArray2 = new float[5][];
                float[] numArray3 = new float[5];
                numArray3[0] = 1f;
                numArray2[0] = numArray3;
                float[] numArray4 = new float[5];
                numArray4[1] = 1f;
                numArray2[1] = numArray4;
                float[] numArray5 = new float[5];
                numArray5[2] = 1f;
                numArray2[2] = numArray5;
                float[] numArray6 = new float[5];
                numArray6[3] = ((float) this.LucencyPercent) / 100f;
                numArray2[3] = numArray6;
                float[] numArray7 = new float[5];
                numArray7[4] = 1f;
                numArray2[4] = numArray7;
                float[][] newColorMatrix = numArray2;
                ColorMatrix matrix = new ColorMatrix(newColorMatrix);
                ImageAttributes imageAttr = new ImageAttributes();
                imageAttr.SetColorMatrix(matrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
                graphics.DrawImage(image2, new Rectangle(x, y, image2.Width, image2.Height), 0, 0, image2.Width, image2.Height, GraphicsUnit.Pixel, imageAttr);
                FileInfo info = new FileInfo(this.ModifyImagePath);
                ImageFormat gif = ImageFormat.Gif;
                switch (info.Extension.ToLower())
                {
                    case ".jpg":
                        gif = ImageFormat.Jpeg;
                        break;

                    case ".gif":
                        gif = ImageFormat.Gif;
                        break;

                    case ".png":
                        gif = ImageFormat.Png;
                        break;

                    case ".bmp":
                        gif = ImageFormat.Bmp;
                        break;

                    case ".tif":
                        gif = ImageFormat.Tiff;
                        break;

                    case ".wmf":
                        gif = ImageFormat.Wmf;
                        break;

                    case ".ico":
                        gif = ImageFormat.Icon;
                        break;
                }
                MemoryStream stream = new MemoryStream();
                image.Save(stream, gif);
                byte[] buffer = stream.ToArray();
                image.Dispose();
                image2.Dispose();
                graphics.Dispose();
                FileStream stream2 = null;
                if (this.OutPath.IsNull() || (this.OutPath == ""))
                {
                    File.Delete(this.ModifyImagePath);
                    stream2 = new FileStream(this.ModifyImagePath, FileMode.Create, FileAccess.Write);
                }
                else
                {
                    stream2 = new FileStream(this.OutPath, FileMode.Create, FileAccess.Write);
                }
                if (stream2.IsNotNull())
                {
                    stream2.Write(buffer, 0, buffer.Length);
                    stream2.Close();
                }
            }
            finally
            {
                try
                {
                    image2.Dispose();
                    image.Dispose();
                    graphics.Dispose();
                }
                catch
                {
                }
            }
        }

        public void DrawText(string watermarkText, int watermarkStatus, int quality, string fontname = "宋体", int fontsize = 12)
        {
            if (FileDirectory.FileExists(this.ModifyImagePath))
            {
                Image image = Image.FromFile(this.ModifyImagePath);
                string filename = this.OutPath.IsNullEmpty() ? this.ModifyImagePath : this.OutPath;
                Graphics graphics = Graphics.FromImage(image);
                Font font = new Font(fontname, (float) fontsize, FontStyle.Regular, GraphicsUnit.Pixel);
                SizeF ef = graphics.MeasureString(watermarkText, font);
                float x = 0f;
                float y = 0f;
                switch (watermarkStatus)
                {
                    case 1:
                        x = image.Width * 0.01f;
                        y = image.Height * 0.01f;
                        break;

                    case 2:
                        x = (image.Width * 0.5f) - (ef.Width / 2f);
                        y = image.Height * 0.01f;
                        break;

                    case 3:
                        x = (image.Width * 0.99f) - ef.Width;
                        y = image.Height * 0.01f;
                        break;

                    case 4:
                        x = image.Width * 0.01f;
                        y = (image.Height * 0.5f) - (ef.Height / 2f);
                        break;

                    case 5:
                        x = (image.Width * 0.5f) - (ef.Width / 2f);
                        y = (image.Height * 0.5f) - (ef.Height / 2f);
                        break;

                    case 6:
                        x = (image.Width * 0.99f) - ef.Width;
                        y = (image.Height * 0.5f) - (ef.Height / 2f);
                        break;

                    case 7:
                        x = image.Width * 0.01f;
                        y = (image.Height * 0.99f) - ef.Height;
                        break;

                    case 8:
                        x = (image.Width * 0.5f) - (ef.Width / 2f);
                        y = (image.Height * 0.99f) - ef.Height;
                        break;

                    case 9:
                        x = (image.Width * 0.99f) - ef.Width;
                        y = (image.Height * 0.99f) - ef.Height;
                        break;
                }
                graphics.DrawString(watermarkText, font, new SolidBrush(Color.White), (float) (x + 1f), (float) (y + 1f));
                graphics.DrawString(watermarkText, font, new SolidBrush(Color.Black), x, y);
                ImageCodecInfo[] imageEncoders = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo info = null;
                foreach (ImageCodecInfo info2 in imageEncoders)
                {
                    if (info2.MimeType.IndexOf("jpeg") > -1)
                    {
                        info = info2;
                    }
                }
                EncoderParameters encoderParams = new EncoderParameters();
                long[] numArray = new long[1];
                if ((quality < 0) || (quality > 100))
                {
                    quality = 80;
                }
                numArray[0] = quality;
                EncoderParameter parameter = new EncoderParameter(Encoder.Quality, numArray);
                encoderParams.Param[0] = parameter;
                if (FileDirectory.FileExists(this.ModifyImagePath))
                {
                    FileDirectory.FileDelete(this.ModifyImagePath);
                }
                if (info.IsNotNull())
                {
                    image.Save(filename, info, encoderParams);
                }
                else
                {
                    image.Save(filename);
                }
                graphics.Dispose();
                image.Dispose();
            }
        }

        public int BottoamSpace
        {
            get
            {
                return this.bottoamSpace;
            }
            set
            {
                this.bottoamSpace = value;
            }
        }

        public string DrawedImagePath
        {
            get
            {
                return this.drawedImagePath;
            }
            set
            {
                this.drawedImagePath = value;
            }
        }

        public int LucencyPercent
        {
            get
            {
                return this.lucencyPercent;
            }
            set
            {
                if ((value >= 0) && (value <= 100))
                {
                    this.lucencyPercent = value;
                }
            }
        }

        public string ModifyImagePath
        {
            get
            {
                return this.modifyImagePath;
            }
            set
            {
                this.modifyImagePath = value;
            }
        }

        public string OutPath
        {
            get
            {
                return this.outPath;
            }
            set
            {
                this.outPath = value;
            }
        }

        public int RightSpace
        {
            get
            {
                return this.rightSpace;
            }
            set
            {
                this.rightSpace = value;
            }
        }
    }
}

